package xw.qxypt.modules.baseinfo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import xw.qxypt.common.utils.MapGPSTranslateUtil;
import xw.qxypt.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xw.qxypt.modules.baseinfo.entity.LhpWaysectRoleEntity;
import xw.qxypt.modules.baseinfo.service.LhpWaysectRoleService;
import xw.qxypt.common.utils.PageUtils;
import xw.qxypt.common.utils.R;



/**
 * 路段角色表
 *
 * @author xw
 * @email 
 * @date 2019-11-28 16:38:08
 */
@RestController
@RequestMapping("baseinfo/lhpwaysectrole")
public class LhpWaysectRoleController {
    @Autowired
    private LhpWaysectRoleService lhpWaysectRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("baseinfo:lhpwaysectrole:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = lhpWaysectRoleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("baseinfo:lhpwaysectrole:info")
    public R info(@PathVariable("id") String id){
        LhpWaysectRoleEntity lhpWaysectRole = lhpWaysectRoleService.getById(id);

        return R.ok().put("lhpWaysectRole", lhpWaysectRole);
    }

    /**
     * 根据登录用户查询路段角色信息
     */
    @RequestMapping("/getByUserId")
    public R getByUserId(){
        List<LhpWaysectRoleEntity> list=lhpWaysectRoleService.getByUserId();
        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("baseinfo:lhpwaysectrole:save")
    public R save(@RequestBody LhpWaysectRoleEntity lhpWaysectRole){
        String lat=lhpWaysectRole.getYGaode();
        String lon=lhpWaysectRole.getXGaode();
        //高德坐标转百度与gps
        if(lat!=null && !"".equals(lat) && lon !=null && !"".equals(lon)){
            //格式化保留统一保留6位小数
            lhpWaysectRole.setXGaode(String.format("%.6f",Double.parseDouble(lon)));
            lhpWaysectRole.setYGaode(String.format("%.6f",Double.parseDouble(lat)));
            double[] gps84= MapGPSTranslateUtil.gcj02_To_Gps84(Double.parseDouble(lat),Double.parseDouble(lon));
            double[] bd09=MapGPSTranslateUtil.gcj02_To_Bd09(Double.parseDouble(lat),Double.parseDouble(lon));
            lhpWaysectRole.setXGps(String.valueOf(gps84[1]));
            lhpWaysectRole.setYGps(String.valueOf(gps84[0]));
            lhpWaysectRole.setXBaidu(String.valueOf(bd09[1]));
            lhpWaysectRole.setYBaidu(String.valueOf(bd09[0]));
        }
        lhpWaysectRoleService.save(lhpWaysectRole);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("baseinfo:lhpwaysectrole:update")
    public R update(@RequestBody LhpWaysectRoleEntity lhpWaysectRole){
        ValidatorUtils.validateEntity(lhpWaysectRole);
        String lat=lhpWaysectRole.getYGaode();
        String lon=lhpWaysectRole.getXGaode();
        //高德坐标转百度与gps
        if(lat!=null && !"".equals(lat) && lon !=null && !"".equals(lon)){
            //格式化保留统一保留6位小数
            lhpWaysectRole.setXGaode(String.format("%.6f",Double.parseDouble(lon)));
            lhpWaysectRole.setYGaode(String.format("%.6f",Double.parseDouble(lat)));
            double[] gps84= MapGPSTranslateUtil.gcj02_To_Gps84(Double.parseDouble(lat),Double.parseDouble(lon));
            double[] bd09=MapGPSTranslateUtil.gcj02_To_Bd09(Double.parseDouble(lat),Double.parseDouble(lon));
            lhpWaysectRole.setXGps(String.valueOf(gps84[1]));
            lhpWaysectRole.setYGps(String.valueOf(gps84[0]));
            lhpWaysectRole.setXBaidu(String.valueOf(bd09[1]));
            lhpWaysectRole.setYBaidu(String.valueOf(bd09[0]));
        }
        lhpWaysectRoleService.updateById(lhpWaysectRole);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("baseinfo:lhpwaysectrole:delete")
    public R delete(@RequestBody String[] ids){
        lhpWaysectRoleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
